const WXAPI = require('apifm-wxapi')
const {getCurrentPage} = require("../../miniprogram_npm/@vant/weapp/common/utils");
Page({
    data: {
        groups: [],
        page: 1,
        pageSize: 10,
        curAddressData: {}
    },
    onLoad: function (options) {
        
        const {latitude,longitude} = options
        this.setData({
            curAddressData: {latitude,longitude},
        })
        this.groupNearby()
    },
    async groupNearby() {

        if (this.data.curAddressData) {
            let latitude = this.data.curAddressData.latitude
            let longitude = this.data.curAddressData.longitude
            
            const groupRes = await WXAPI.groupNearby({latitude, longitude, page: 1, pageSize: this.data.pageSize});
            if (groupRes.code === 0) {
                if (this.data.page === 1) {
                    this.setData({
                        groups: groupRes.data
                    })
                } else {
                    this.setData({
                        groups: this.data.groups.concat(groupRes.data)
                    })
                }

               
            }
        }

    },
    loadMore() {
        this.data.page++
        this.groupNearby()
    },
    onSelect(tap){
        let pages = getCurrentPages();

        let prev = pages[pages.length -2]
        prev.setData({selectedGroup: tap.target.dataset.groupid})
        wx.navigateBack({
            delta:1,
        })
    }
});